import dayjs from "dayjs";
import { STANDARD_CYCLE_TIME } from "../../../constant";
import { AbstractPlantStruct } from "./AbstractPlantStruct";
import type { PlanetPin } from "@scvzerng/esi-163/dist/types/esi/plants/model/PlanetLayout";
import type { UniverseType } from "../../../sde/entity";

/**
 * 行星开发采集器
 */
export class PlanetCollector extends AbstractPlantStruct {
  /**
   * 失效时间
   */
  expiryTime?: string;

  /**
   * 最后一次循环时间
   */
  lastCycleStart?: string;
  /**
   * 采集资源类型
   */
  fetchType?: UniverseType;
  /**
   * 每循环周期（秒）
   */
  cycleTime?: number;
  /**
   * 每循环数量
   */
  perCycleCount?: number;

  constructor(pin: PlanetPin, type: UniverseType, productType?: UniverseType) {
    super(pin, type);
    this.expiryTime = pin.expiry_time;
    this.lastCycleStart = pin.last_cycle_start;
    this.fetchType = productType;
    this.cycleTime = pin.extractor_details?.cycle_time;
    this.perCycleCount = pin.extractor_details?.qty_per_cycle;
  }

  /**
   * 获取有效周期内产出的资源
   */
  get totalFetchResource() {
    return Math.ceil(this.cycleCount * this.perCycleCount!);
  }

  /**
   * 循环采集次数
   */
  get cycleCount() {
    const start = dayjs(this.lastCycleStart);
    const stop = dayjs(this.expiryTime);
    const duration = stop.diff(start, "seconds");
    if (!this.cycleTime || !this.perCycleCount) {
      return 0;
    }
    return duration / this.cycleTime;
  }

  /**
   * 标准循环每次采集数量
   */
  get standardPerCycleCount() {
    //没有启动采集器
    if (!this.cycleTime || !this.perCycleCount) return 0;
    const factor = STANDARD_CYCLE_TIME / this.cycleTime;
    return Math.ceil(this.perCycleCount * factor);
  }
}
